#ifndef _DL_H_
#define _DL_H_

#include "../os.h"

/*
OBS: no linux, precisa ser compilado com -rdynamic ou -Wl,-E ou -export-dynamic
para o dlopen/dlsym poder ler os simbolos do proprio executavel. No windows, a
definicao do EXPORT em interface.h eh o suficiente para GetModuleHandleEx/GetProcAddress.
*/

#ifdef DL
  #if ISWIN
    #define WIN_DL
  #elif ISUNIX
    #define UNIX_DL
  #elif ISLINUX
    #define UNIX_DL
  #endif
#endif

#ifdef UNIX_DL
	#include <dlfcn.h>
	typedef void (*FNPTR)(void);
	typedef void* LibHandler;
	#define LibOpen(name) (LibHandler)dlopen(name,RTLD_LAZY)
	#define LibFind(handler, fname) (FNPTR)dlsym(handler, fname)
	#define LibClose(handler) dlclose(handler)
	#define fillCurrentProccess(procVar) (procVar = (LibHandler)dlopen(0,RTLD_LAZY))
#endif

#ifdef WIN_DL
    #define _WIN32_WINNT 0x0501
	#include <windows.h>
	typedef void (*FNPTR)(void);
	typedef HMODULE LibHandler;
	#define LibOpen(name) (LibHandler)LoadLibrary(name)
	#define LibFind(handler, fname) (FNPTR)GetProcAddress(handler, fname)
	#define LibClose(handler) FreeLibrary(handler)
	#define fillCurrentProccess(procVar) (GetModuleHandleEx(0,0,&procVar))
#endif

#ifndef LibOpen
    typedef void (*FNPTR)(void);
    typedef void* LibHandler;
    #define LibOpen(name) (LibHandler)0
    #define LibFind(handler, fname) (FNPTR)0
    #define LibClose(handler)
    #define fillCurrentProccess(procVar)
#endif

LibHandler getCurrentProccesHandler();

#endif